/*
 * Fichier: ChapterPane.java
 * Crée le: 15 septembre 2007.
 * Modifié: 26 avril 2012.
 * Auteurs: Sébastien ESTIENNE.
 * SiteWeb: http://www.prog-info.org/
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.OF
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package javaexos.gui;



import java.awt.BorderLayout;

import javaexos.controller.ChapterController;
import javaexos.gui.common.GLink;
import javaexos.model.ChapterModel;
import javaexos.tools.Constants;
import javaexos.tools.HTMLConstants;
import javaexos.view.ChapterView;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;



/**
 * <p>Panneau d'informations d'un chapitre.</p>
 * @author Sébastien ESTIENNE.
 */
public class ChapterPane extends JPanel implements ChapterView
{
   /**
    * <p>Serial version UID.</p>
    */
   private static final long serialVersionUID = 1L;

   /** Les informations d'un chapitre. */
   private ChapterModel chapter;

   /** Le contrôleur. */
   @SuppressWarnings("unused")
   private ChapterController controller;

   /** Texte du chapitre. */
   private JTextPane chapterText;
   
   /** Editeur de Kit HTML. */
   private HTMLEditorKit htmlKit;
   
   /** Document HTML. */
   private HTMLDocument htmlDoc;
   
   /** Feuille de style. */
   private StyleSheet styles;
   
   /**
    * <p>Construit le panneau pour un chapitre.</p>
    * @param chapter Les informations d'un chapitre.
    * @param controller Le contrôleur associé.
    */
   public ChapterPane(ChapterModel chapter, ChapterController controller)
   {
      super();

      // Initialisation.
      this.chapter = chapter;
      this.controller = controller;
      
      initComponents();
   }


   /**
    * <p>Initialise le panneau pour l'affichage des chapitres.</p>
    */
   private void initComponents()
   {
      setLayout(new BorderLayout(5, 5));

      // Initialise l'éditeur de kit HTML.
      this.htmlKit = new HTMLEditorKit();
      
      // Initialise la feuille de style.
      this.styles = new StyleSheet();
      this.styles.importStyleSheet(getClass().getResource(Constants.JAVAEXOS_STYLESHEET));
      this.htmlKit.setStyleSheet(this.styles);
      
      // Initialise le document HTML.
      this.htmlDoc = (HTMLDocument) this.htmlKit.createDefaultDocument();
      
      // Définit le panneau de texte du chapitre.
      this.chapterText = new JTextPane();
      this.chapterText.setEditable(false);
      this.chapterText.setFocusable(false);
      this.chapterText.setEditorKit(this.htmlKit);
      this.chapterText.setDocument(this.htmlDoc);
      this.chapterText.setContentType("text/html");
      this.chapterText.addHyperlinkListener(new GLink(this));

      add(new JScrollPane(this.chapterText), BorderLayout.CENTER);
   }

   /**
    * <p>Modifie le chapitre courant du panneau et actualise le panneau.</p>
    * @param chapter Le nouveau chapitre.
    */
   public void setChapter(ChapterModel chapter)
   {
      this.chapter = chapter;
      
      SwingUtilities.invokeLater(new Runnable()
      {
         public void run()
         {
            updatePane();
         }
      });
   }

   /**
    * <p>Met à jour le panneau.</p>
    */
   private void updatePane()
   {
      // Affiche les informations du chapitre.
      StringBuilder content = new StringBuilder();
      content.append(HTMLConstants.HTML_OPEN_MARKER);
      
      // Head.
      content.append(HTMLConstants.HEAD_OPEN_MARKER);
      content.append(HTMLConstants.TITLE_OPEN_MARKER);
      content.append("Chapitre ").append(ChapterPane.this.chapter.getNumber());
      content.append(HTMLConstants.HEAD_OPEN_MARKER);
      
      // Body.
      content.append(HTMLConstants.BODY_OPEN_MARKER);
      
      // Titre.
      content.append(HTMLConstants.H1_OPEN_MARKER);
      content.append(ChapterPane.this.chapter.getTitle());
      content.append(HTMLConstants.H1_CLOSED_MARKER);
      
      // Présentation.
      content.append(HTMLConstants.H2_OPEN_MARKER);
      content.append("Présentation :");
      content.append(HTMLConstants.H2_CLOSED_MARKER);
      
      String presentation = ChapterPane.this.chapter.getPresentation();
      content.append(HTMLConstants.P_OPEN_MARKER);
      content.append(presentation);
      content.append(HTMLConstants.P_CLOSED_MARKER);
      
      // Notions.
      content.append(HTMLConstants.H2_OPEN_MARKER);
      content.append("Notions :");
      content.append(HTMLConstants.H2_CLOSED_MARKER);
      
      if(!ChapterPane.this.chapter.getNotions().isEmpty())
      {
         content.append(HTMLConstants.UL_OPEN_MARKER);
         for(String notion : ChapterPane.this.chapter.getNotions())
         {
            content.append(HTMLConstants.LI_OPEN_MARKER);
            content.append(notion);
            content.append(HTMLConstants.LI_CLOSED_MARKER);
         }
         content.append(HTMLConstants.UL_CLOSED_MARKER);
      }
      
      // Lien vers l'article en ligne.
      content.append("<p></p>");
      content.append("<table cellspacing=\"3\"><tr><td>");
      content.append("<img src=\"" + getClass().getResource(Constants.LINK_ICON_PATH) 
            + "\" hspace=\"12px;\">");
      content.append("</td><td>");
      content.append("<a href=\"" + Constants.CHAPTER_WEB_ADDRESS 
            + ChapterPane.this.chapter.getName() 
            + "\">Retrouver cet article sur developpez.com</a>");
      content.append("</td></tr></table>");
       
      // Fin du document.
      content.append(HTMLConstants.BODY_CLOSED_MARKER);
      content.append(HTMLConstants.HTML_CLOSED_MARKER);
      
      // Modifie le contenu.
      ChapterPane.this.chapterText.setText(content.toString());
      ChapterPane.this.chapterText.setCaretPosition(0);
   }
}
